//
//  ProductCell.m
//  TableViewSquaredDemo
//
//  Created by Rannie on 13-9-10.
//  Copyright (c) 2013年 Rannie. All rights reserved.
//

#import "ProductCell.h"
#import "ProductButton.h"
#import "Product.h"

#define RCellWidth 320
#define RMarginX 8
#define RMarginY 4
#define RStartTag 100

@implementation ProductCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self initButtons];
    }
    return self;
}

- (void)initButtons
{
    //MyLog(@"初始化按钮");
    NSInteger width = RCellWidth/RColCount;
    for (NSInteger i = 0; i < RColCount; i++)
    {
        ProductButton *btn = [[ProductButton alloc] init];
        btn.frame = CGRectMake(i*width + RMarginX, RMarginY, width - 2*RMarginX, RCellHeight - 2*RMarginY);
        btn.tag = RStartTag + i;
        [self.contentView addSubview:btn];
    }
}

- (void)bindProducts:(NSArray *)productList
{
    for (NSInteger i = 0; i < RColCount; i++)
    {
        ProductButton *btn = (ProductButton *)[self.contentView viewWithTag:RStartTag + i];
        Product *product = productList[i];
        btn.tag = product.flag;
        [btn setImage:[UIImage imageNamed:product.imageName] forState:UIControlStateNormal];
        [btn setTitle:product.title forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
    }
}

- (void)buttonTapped:(ProductButton *)sender
{
    //MyLog(@"%d", sender.tag);
    [_cellDelegate productCell:self actionWithFlag:sender.tag];
}

@end
